<template>
  <div>
    <!-- 导航栏 -->
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contnetScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <!-- 轮播图 -->
      <home-swiper :banner="banner" />
      <!-- Recommend -->
      <recommend-view :recommend="recommend" />
      <!-- 特色 -->
      <feature-view />
      <!--  tab切换  -->
      <tab-control @tabClick="tabClick" :titles="['流行', '新款', '精选']" />
      <!-- 商品列表 -->
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- // 返回顶部 -->
    <!-- 监听组件原生时间  需要加修饰符 native -->
    <back-top @click.native="backclick" v-show="isBackTopShow" />
  </div>
</template>

<script>
// 导航组件
import NavBar from "components/common/navbar/NavBar";
// 分类组件
import TabControl from "components/content/tabControl/TabControl";
// 商品列表组件
import GoodsList from "components/content/goods/GoodsList";
// scroll组件
import Scroll from "components/common/scroll/Scroll";
// 返回顶部
import BackTop from "components/content/backTop/BackTop";

// 轮播图组件
import HomeSwiper from "./childComps/HomeSwiper";
// recommend
import RecommendView from "./childComps/RecommendView";
// 中间图片组件
import FeatureView from "./childComps/FeatureView";

// 请求方法
import { getHomeMultidata, getHomeGoods } from "network/home";
export default {
  components: {
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
    HomeSwiper,
    RecommendView,
    FeatureView
  },
  data() {
    return {
      banner: [],
      recommend: [],
      //   商品数据
      goods: {
        pop: {
          page: 0,
          list: []
        },
        new: {
          page: 0,
          list: []
        },
        sell: {
          page: 0,
          list: []
        }
      },
      // 当前类型
      currentType: "pop",
      // 是否显示回到顶部按钮
      isBackTopShow: false
    };
  },
  created() {
    //   获取banner
    this.getHomeMultidata();
    // 获取商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("sell");
    this.getHomeGoods("new");
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    }
  },
  methods: {
    //   获取banner
    async getHomeMultidata() {
      const res = await getHomeMultidata();
      const { banner, recommend } = res.data;
      this.banner = banner.list;
      this.recommend = recommend.list;
    },
    // 获取商品数据
    async getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      const res = await getHomeGoods(type, page);
      this.goods[type].list.push(...res.data.list);
      this.goods[type].page++;
      console.log(this.goods[type]);
    },
    // 点击tab切换数据
    tabClick(index) {
      // console.log(index)
      // 根据索引判断需要显示的类型
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
    },
    // 回到顶部
    backclick() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    // 回到顶部  显示/隐藏
    contnetScroll(position) {
      // let num = Math.abs(position.y);
      // this.isBackTopShow = num > 1000;
      // console.log(position.y);
    },
    // 上拉加载更多
    loadMore() {
      console.log(212);
      this.getHomeGoods(this.currentType);
    }
  }
};
</script>

<style lang="less" scoped>
.home-nav {
  background: var(--color-tint);
  color: #fff;
}
.van-swipe-item {
  img {
    width: 100% !important;
  }
  padding-top: 44px;
}
.tab-control {
  position: sticky;
  top: 44px;
}
.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}
</style>
